<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/4/1
 * Time: 15:53
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Common\Tools\AppVersion;
use SDXApi\Model\SeminarTemplateBagModel;

/**
 * Class SeminarBagService
 * @package SDXApi\Service
 */
class SeminarBagService extends TemplateAbstract
{
	/**
	 * @var SeminarTemplateBagModel
	 */
	protected $seminarBagModel;

	/**
	 * SeminarBagService constructor.
	 */
	public function __construct()
	{
		$this->seminarBagModel = new SeminarTemplateBagModel();
	}

	/**
	 * Created by Jerry
	 * @param array $template
	 * @param int $userId
	 * @param int $wid
	 * @return array|bool|mixed
	 */
	public function handle($template = array(), $userId = 0, $wid = 8)
	{
	    $appVersion = AppVersion::getAppVersion();
		$cacheName = 'seminar_template_page_bag_' . $template['id'] . '_app' . $appVersion['app'] . '_version_' . $appVersion['version'];
		$data = $this->getCache($cacheName);

		if (empty($data)) {
			$data = $this->bag($template);

			if (! empty($data)) {
				$this->cacheData($cacheName, $data, mt_rand(3600, 4200));
			}
		}

		//检查当前红包是否已领取
        $res = A('H5user')->checkBonusIsReceive($data['bag_id']);
        $data['action'] = $res['action'];

		return $data;
	}

	/**
	 * Created by Jerry
	 * @param array $page
	 * @return array|bool
	 */
	protected function bag($page = array())
	{
		$bagData = $this->seminarBagModel->get_seminar_template_bag_by_id($page['id']);

		if (empty($bagData)) {
			return false;
		}

		$page['image_start'] 	= $bagData['image_start'];
		$page['image_end'] 		= $bagData['image_end'];
		$page['bag_link'] 		= $bagData['bag_link'];
		$page['bag_id']			= $bagData['bag_id'];
		$page['end_height']     = $bagData['end_height'];
		$page['end_width']      = $bagData['end_width'];
		$page['start_width']    = $bagData['start_width'];
		$page['start_height']   = $bagData['start_height'];

        $page['bag_size']		= '750 * 900';
		if (AppVersion::checkAppVersion('ios', '2.3.80', '>=')) {
            $page['bag_size']		= $bagData['start_width'] . ' * ' . $bagData['start_height'];
        }

		unset($page['font_type'], $page['font_size'], $page['font_content'], $page['font_link'], $page['image_url'], $page['image_link'], $page['image_size'], $page['goods_type'], $page['goods_id'], $page['template_id']);

		return $page;
	}

}